<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      title="文章详情"
      @click-left="$router.back('/')"
    />
    <!-- 文章详情 -->
    <div v-if="loading" class="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>
    <div v-else class="detail">
      <h3 class="title">{{ details.title }}</h3>
      <div class="author">
        <van-image :src="details.aut_photo" round width="1rem" height="1rem" fit="cover" />
        <div class="text">
          <p class="name">{{ details.aut_name }}</p>
          <p class="time">{{ details.pubdate|fromNow }}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="isFollow(details.art_id.toString())"
        >{{ !details.is_followed?'- 取关':'+ 关注' }}</van-button>
      </div>
      <div class="content">
        <div v-html="details.content">嘻嘻嘻嘻</div>
      </div>

      <van-divider>END</van-divider>

      <div class="zan">
        <van-button round size="small" hairline type="primary" plain icon="good-job-o" @click="like()">
          {{ details.attitude===0?'取消':'点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete" @click="dislike()">
          {{ details.attitude===0?'就这':'取消' }}
        </van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <Comment />
  </div>
</template>

<script>
import Comment from '@/views/article/comment.vue'

import { reqGetNewDetails, followUser, unfollowUser, deleteLike, addLike, reqDeleteDisLike, reqAddDisLike } from '@/api/article'
export default {
  name: 'Article',
  components: {
    Comment
  },
  data() {
    return {
      loading: true,
      details: ''
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  async created() {
    const res = await reqGetNewDetails(this.id)
    this.details = res.data
    console.log(this.details.attitude)
    this.loading = !this.loading
  },
  methods: {
    // 需求：点击关注功能
    // 1.是否登录  是登录发送关注请求  2.判断 detalils的follow为true发送取关 为false 发送关注请求
    //             否跳转到login页面
    async isFollow() {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'true'
          }
        })
        return
      }
      if (this.details.is_followed) {
        const res = await unfollowUser(this.details.aut_id)
        console.log('un', res)
      } else {
        await followUser(this.details.aut_id)
      }
      this.details.is_followed = !this.details.is_followed
    },
    async like() {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'true'
          }
        })
        return
      }
      if (this.details.attitude === 1) {
        await deleteLike(this.id)
      } else {
        await addLike(this.id)
      }
      this.details.attitude = this.details.attitude === 1 ? 0 : 1
    },
    async dislike() {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'true'
          }
        })
        return
      }
      if (this.details.attitude === 0) {
        console.log('reqDeleteDisLike')
        await reqDeleteDisLike(this.id)
      } else {
        await reqAddDisLike(this.id)
        console.log('reqAddDisLikes')
      }
      console.log('dislike', this.details.attitudes)
      this.details.attitude = this.details.attitude === 0 ? -1 : 0
    }

  }

}
</script>

<style lang='less' scoped>
.loading {
  height: 100vh;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
